package edu.math.chaos.ui;

import edu.math.chaos.params.ApplicationParameters;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;

/**
 * User: Илья
 * Date: 21.11.13
 * Time: 21:57
 */
public class UIApplication extends Application {

	public void start(Stage stage) throws Exception {
		UIApplication.stage = stage;
		FXMLLoader loader = new FXMLLoader(getClass().getResource(FXML_PATH));
		Parent parent = (Parent) loader.load();
		final DrawBoardController controller = loader.getController();
		stage.addEventHandler(WindowEvent.WINDOW_SHOWN, new EventHandler<WindowEvent>() {

			public void handle(WindowEvent windowEvent) {
				controller.draw();
			}
		});

		stage.setScene(new Scene(parent, WIDTH, HEIGHT));
		stage.setTitle(TITLE);
		stage.show();
	}

	private static final String FXML_PATH = "DrawBoard.fxml";
	private static final int WIDTH = 700;
	private static final int HEIGHT = 700;
	private static final String TITLE = "Внешняя часть множества Жюлиа";

	private static Stage stage;
	private static ApplicationParameters parameters;

	public static void main(String[] args) {
		if (args.length > 0) {
			parameters = ApplicationParameters.readParameters(args[0]);
		}

		launch(args);
	}

	public static ApplicationParameters getApplicationParameters() {
		return parameters;
	}

	public static void close() {
		if (stage != null) {
			stage.close();
		}
	}
}
